package cn.echo.ecc.web.controller;

import cn.echo.ecc.service.IRecommendCodeService;
import cn.echo.ecc.domain.RecommendCode;
import cn.echo.ecc.query.RecommendCodeQuery;
import cn.echo.ecc.result.JSONResult;
import cn.echo.ecc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
@RequestMapping("/recommendCode")
public class RecommendCodeController {

    @Autowired
    public IRecommendCodeService recommendCodeService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody RecommendCode recommendCode){
        if(recommendCode.getId()!=null){
            recommendCodeService.updateById(recommendCode);
        }else{
            recommendCodeService.insert(recommendCode);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        recommendCodeService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(recommendCodeService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(recommendCodeService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody RecommendCodeQuery query){
        Page<RecommendCode> page = new Page<RecommendCode>(query.getPage(),query.getRows());
        page = recommendCodeService.selectPage(page);
        return JSONResult.success(new PageList<RecommendCode>(page.getTotal(),page.getRecords()));
    }

    /**
     * 获取推荐链接
     */
    @RequestMapping(value = "/getCode",method = RequestMethod.POST)
    public JSONResult getCode(){
        return JSONResult.success(recommendCodeService.getCode());
    }

    /**
     * 获取推荐链接二维码
     */
    @RequestMapping(value = "/getQRCode",method = RequestMethod.POST)
    public JSONResult getQRCode() {
        return JSONResult.success(recommendCodeService.getQRCode());
    }
}
